<?php
namespace App\Services\IntegralGoods;

use App\Services\IntegralGoods\Goods\GoodsLogic;
use App\Services\IntegralGoods\Goods\GoodsCategoryLogic;
use App\Services\User\Token;
use App\Services\User\App;
use App\Services\Coding\Coding;
use App\Services\IntegralGoods\ValidateGoods;
use App\Services\Manage\Api;

/**
 * Class Goods 商品服务
 * @package App\Services\Goods
 */
class Goods
{
    /*艺货后台发布/变价积分商品时，对基本数据以及分类的验证*/
    protected function _checkData($data, $scene = ''): array
    {
//        $validate = validate($this->goodsValidate);
//        if(!$validate->scene($scene)->check($data)){  return verify_err(null, $validate->getError());  }
        if(isset($data['cate_id'])){
            $checkCate = (new GoodsCategoryLogic())->checkReadCate(['id'=>$data['cate_id'], 'app_id'=>$data['app_id']]);
            if(!$checkCate){  return verify_err(null, '该商品分类的状态错误');  }
        }
        return auto_success($data, '检验数据正常');
    }

    /*艺货后台发布积分商品*/
    public function addIntegralGoods($data): array
    {
        $data = $this->_returnAppId($data);
        $token = (new Token())->hGetAll($data['token']);
        $code = (new Coding())->getCoding(['type'=>'goods','token'=>$data['token'],'app_id'=>$data['app_id']]);
        $data['user_id'] = $token['res']['id'];
        $data['product_code'] = $code['res']['autoRes'];
        ValidateGoods::checkValidate($data,['cate_id', 'title', 'integral', 'profile_pic', 'imgs_urls', 'imgs_ids', 'origin_number', 'order', 'desc', 'token']);
        $checkData = $this->_checkData($data);
        if($checkData['status'] != SUCCESS_STATUS){ return $checkData; }
        return (new GoodsLogic())->addIntegralGoods($data);
    }

//    /*艺货后台发布积分商品*/
//    public function addIntegralGoods(): array
//    {
//        $data = $this->haveIssetReceiveData(['app_id'=>0,'user_id'=>0, 'cate_id'=>0, 'title'=>' ', 'integral'=>0, 'product_code'=>' ', 'profile_pic'=>'', 'imgs_urls'=>' ', 'imgs_ids'=>' ', 'origin_number'=>0, 'order'=>0, 'desc'=>' ']);
//
//        $checkData = $this->_checkData($data);
//        if($checkData['status'] != SUCCESS_STATUS){ return $checkData; }
//
//        return $this->logic($this->goodsLogic)->addIntegralGoods($data);
//    }

    /*后台修改积分商品*/
    public function updateIntegralGoods($data): array
    {
        $data = $this->_returnAppId($data);
        ValidateGoods::checkValidate($data,['id', 'app_id', 'cate_id', 'title', 'integral', 'profile_pic', 'imgs_urls', 'imgs_ids', 'number', 'origin_number', 'order', 'desc']);
        $checkData = $this->_checkData($data);
        if($checkData['status'] != SUCCESS_STATUS){ return $checkData; }
        return (new GoodsLogic())->updateIntegralGoods($data);
    }

    /*艺货后台获取积分商品分页列表*/
    public function allGoodsList($data): array
    {
        $data = $this->_returnAppId($data);
        validateGoods::checkValidate($data,['app_id', 'keywords', 'page', 'listRows']);
        return (new GoodsLogic())->allGoodsList($data);
    }

    /*管理员下架某一个商品*/
    public function soldOutOneGoods($data): array
    {
        $data = $this->_returnAppId($data);
        $data['id'] = [$data['id']];
        $data['status'] = 3;
        ValidateGoods::checkValidate($data,['id', 'token', 'app_id'], true, ['id'=>true]);
        $checkGoodsRes = $this->_adminBatchCheckGoods($data, 'manageBatchSoldOutGoods');
        if($checkGoodsRes['status'] != SUCCESS_STATUS){
            return $checkGoodsRes;
        }
        return (new GoodsLogic())->batchOperaGoods($data, 'manageBatchSoldOutGoods');
    }

    /*管理员下架某一批商品*/
    public function soldOutBatchGoods($data): array
    {
        $data = $this->_returnAppId($data);
        $data['status'] = 3;
        ValidateGoods::checkValidate($data,['id', 'token', 'app_id'], true, ['id'=>true]);
        $checkGoodsRes = $this->_adminBatchCheckGoods($data, 'manageBatchSoldOutGoods');
        if($checkGoodsRes['status'] != SUCCESS_STATUS){
            return $checkGoodsRes;
        }
        return (new GoodsLogic())->batchOperaGoods($data, 'manageBatchSoldOutGoods');
    }

//    /*管理员批量操作（下架）商品,不包含删除*/
//    protected function _adminBatchOperaGoods($data, $operate): array
//    {
//
//        $validate = validate($this->goodsValidate);
//        if(!$validate->scene($operate)->check($data)){
//            return verify_err(null, $validate->getError());
//        }
//
//        $checkGoodsRes = $this->_adminBatchCheckGoods($data, $operate);
//        if($checkGoodsRes['status'] != SUCCESS_STATUS){
//            return $checkGoodsRes;
//        }
//
//        return $this->logic($this->goodsLogic)->batchOperaGoods($data, $operate);
//    }
//
    /*管理员批量操作商品时，对商品批量检验是否可被操作 $data['id'] = [1,3] */
    protected function _adminBatchCheckGoods(Array $data, $operate = ''): array
    {
        $goodsList = GoodsLogic::dbGoodsList(['id'=>$data['id']]);
        if(count($goodsList) == 0){
            return verify_err(null,'没有要操作的商品');
        }
        if(count($goodsList) != count($data['id'])){
            return verify_err(null,'其中包含不可执行该操作的商品');
        }
        foreach ($goodsList as $k => $v) {
            if($operate == 'manageBatchSoldOutGoods' && !in_array($v['status'], ['1','4'])){
                return verify_err(null,'失败！标题为'.$v['title'].'的商品不可下架');
            }
        }
        return auto_success(null,'该批商品可被操作');
    }

    /*管理员删除商品*/
    public function manageDelGoods($data): array
    {
        $data = $this->_returnAppId($data);
        ValidateGoods::checkValidate($data,['id', 'token', 'app_id']);
        return (new GoodsLogic())->manageDelGoods($data);
    }

    /*管理员批量删除商品*/
    public function manageBatchDelGoods($data): array
    {
        $data = $this->_returnAppId($data);
        ValidateGoods::checkValidate($data,['id', 'token', 'app_id'], true, ['id'=>true]);
        return (new GoodsLogic())->manageDelGoods($data, 'manageBatchOperaGoods');
    }

//    /*前台获取积分商品分页列表*/
//    public function frontGoodsList(): array
//    {
//        $data = $this->haveIssetReceiveData(['app_id'=>0, 'page'=>1, 'listRows'=>15, 'keywords'=>'']);
//        return $this->stepRun($data, 'frontGoodsList');
//    }
//
//    /*前台获取某积分商品的详情*/
//    public function frontGoodsInfo(): array
//    {
//        $data = $this->haveIssetReceiveData(['app_id'=>0, 'id'=>0]);
//        return $this->stepRun($data, 'frontGoodsInfo');
//    }

    private function _returnAppId($data){
        $app_key = Api::getAppKey();
        $find = (new App())->find(['app_key'=>$app_key]);
        $data['app_id'] = $find['res']['id'];
        return $data;
    }


}
